//package com.gpframework.schedule;
//
//import cn.hutool.core.convert.Convert;
//import cn.hutool.core.lang.TypeReference;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.scheduling.annotation.EnableAsync;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
///**
// * gpframework
// * ==================================================================
// * CopyRight © 2017-2099 广州宏略信息科技有限公司
// * 官网地址：http://www.greatplan.com.cn
// * <p>
// * ------------------------------------------------------------------
// * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下
// * 对本程序代码进行修改和使用；不允许对本程序代码以任何目的的再发布。
// * ==================================================================
// *
// * @ClassName ScheduleTask
// * @Author gpframework
// * @Date 2020-12-30
// * @Version 1.0.0
// * EnableScheduling : 1.开启定时任务
// * EnableAsync :2.开启多线程
// */
//@Component
//@EnableScheduling
//@EnableAsync
//@Slf4j
//@ConditionalOnProperty(name = "gpframework.taskPositionDataEnable")
//public class ScheduleTask {
//
//    @Autowired
//    private IGpGoodOrderService iGpGoodOrderService;
//    @Autowired
//    private IGpGoodOrderItemService iGpGoodOrderItemService;;
//
//    /**
//     * 订单自动取消任务开始
//     * 二分钟执行一次
//     */
//    @Scheduled(cron = "0 */02 * * * ?")
//    public void orderCancel() {
//        log.info("------订单自动取消任务开始-------");
//        LambdaQueryWrapper<GpGoodOrder> queryWrapper = new LambdaQueryWrapper<GpGoodOrder>();
//        queryWrapper.eq(GpGoodOrder::getOrderStats, OrderStatusEnum.ORDERSTATUS0.getCode());
//        queryWrapper.le(GpGoodOrder::getEndTime, new Date());
//        List<GpGoodOrder> list = iGpGoodOrderService.list(queryWrapper);
//        log.info("------订单自动取消数量：" + list.size() + "-------");
//        for (GpGoodOrder orderInfo : list) {
//            iGpGoodOrderService.orderCancel(orderInfo);
//        }
//        log.info("------订单自动取消任务结束-------");
//    }
//
//    /**
//     * 订单自动计算金额任务开始 TODO 温馨提醒：小程序更新了之后请注释这个定时任务
//     * 二分钟执行一次
//     */
//    @Scheduled(cron = "0 */01 * * * ?")
//    public void orderItemsPrice() {
//        log.info("------订单自动计算金额任务开始-------");
//        List<GpGoodOrderItem> itemsNew = new ArrayList<>();
//        List<GpGoodOrder> orderListNew = new ArrayList<>();
//        //涉及多个供应商  按比例计算
//        List<GpGoodOrder> orderList = iGpGoodOrderService.orderNo3();
//        GpGoodOrderItem itemNew;
//        GpGoodOrder orderNew;
//
//        for (GpGoodOrder goodOrder : orderList) {
//            List<GpGoodOrderItemVo> itemVos = iGpGoodOrderItemService.listVo(new QueryWrapper<GpGoodOrderItem>().eq("gp.del_status",0).in("gp.order_id", goodOrder.getId()).orderByDesc("unit_price"));
//            List<GpGoodOrderItem> items = Convert.convert(new TypeReference<List<GpGoodOrderItem>>() {}, itemVos);
//            iGpGoodOrderService.setItemPrice(goodOrder,items,true);
////            itemsNew.addAll(items);
//            goodOrder.setSettlementFlag("1");
//
//            for (GpGoodOrderItem item : items) {
//                itemNew = new GpGoodOrderItem();
//                itemNew.setId(item.getId());
//                itemNew.setUnitPrice(item.getUnitPrice());
//                itemNew.setIntegral(item.getIntegral());
//                itemNew.setAllPrice(item.getAllPrice());
//                itemsNew.add(itemNew);
//            }
//            orderNew = new GpGoodOrder();
//            orderNew.setId(goodOrder.getId());
//            orderNew.setSettlementFlag(goodOrder.getSettlementFlag());
//            orderListNew.add(orderNew);
//        }
////        orderListNew.addAll(orderList);
//        //单个供应商  按顺序分配
//        List<GpGoodOrder> orderList4 = iGpGoodOrderService.orderNo4();
//        for (GpGoodOrder goodOrder : orderList4) {
//            List<GpGoodOrderItemVo> itemVos = iGpGoodOrderItemService.listVo(new QueryWrapper<GpGoodOrderItem>().eq("gp.del_status",0).in("gp.order_id", goodOrder.getId()).orderByDesc("unit_price"));
//            List<GpGoodOrderItem> items = Convert.convert(new TypeReference<List<GpGoodOrderItem>>() {}, itemVos);
//            iGpGoodOrderService.setItemPrice(goodOrder,items,false);
////            itemsNew.addAll(items);
//            goodOrder.setSettlementFlag("1");
//
//
//            for (GpGoodOrderItem item : items) {
//                itemNew = new GpGoodOrderItem();
//                itemNew.setId(item.getId());
//                itemNew.setUnitPrice(item.getUnitPrice());
//                itemNew.setIntegral(item.getIntegral());
//                itemNew.setAllPrice(item.getAllPrice());
//                itemsNew.add(itemNew);
//            }
//            orderNew = new GpGoodOrder();
//            orderNew.setId(goodOrder.getId());
//            orderNew.setSettlementFlag(goodOrder.getSettlementFlag());
//            orderListNew.add(orderNew);
//        }
////        orderListNew.addAll(orderList4);
//        //处理部分退款订单
//        List<GpGoodOrderItem> refunItems = iGpGoodOrderItemService.list(new LambdaQueryWrapper<GpGoodOrderItem>().ne(GpGoodOrderItem::getOrderStats, OrderStatusEnum.ORDERSTATUS5.getCode()).isNotNull(GpGoodOrderItem::getRefundId));
//        for (GpGoodOrderItem refunItem : refunItems) {
//            refunItem.setOrderStats(OrderStatusEnum.ORDERSTATUS5.getCode());
//            itemNew = new GpGoodOrderItem();
//            itemNew.setId(refunItem.getId());
//            itemNew.setOrderStats(refunItem.getOrderStats());
//            itemsNew.add(itemNew);
//        }
//
////        itemsNew.addAll(refunItems);
//        //处理微信支付金额计算
//        List<GpGoodOrder> list = iGpGoodOrderService.list(new LambdaQueryWrapper<GpGoodOrder>().eq(GpGoodOrder::getSettlementFlag,"0").eq(GpGoodOrder::getPayType, "1"));
//        for (GpGoodOrder goodOrder : list) {
//            List<GpGoodOrderItemVo> itemVos = iGpGoodOrderItemService.listVo(new QueryWrapper<GpGoodOrderItem>().eq("gp.del_status",0).in("gp.order_id", goodOrder.getId()).orderByDesc("unit_price"));
//            List<GpGoodOrderItem> items = Convert.convert(new TypeReference<List<GpGoodOrderItem>>() {}, itemVos);
//            for (GpGoodOrderItem item : items) {
//                item.setUnitPrice(item.getAllPrice());
//                item.setIntegral(BigDecimal.ZERO);
//
//                itemNew = new GpGoodOrderItem();
//                itemNew.setId(item.getId());
//                itemNew.setUnitPrice(item.getUnitPrice());
//                itemNew.setIntegral(item.getIntegral());
//                itemsNew.add(itemNew);
//            }
////            itemsNew.addAll(items);
//            goodOrder.setSettlementFlag("1");
//
//            orderNew = new GpGoodOrder();
//            orderNew.setId(goodOrder.getId());
//            orderNew.setSettlementFlag(goodOrder.getSettlementFlag());
//            orderListNew.add(orderNew);
//        }
////        orderListNew.addAll(list);
//        log.info("需要处理订单数量： " + orderListNew.size());
//        if (orderListNew.size() > 0  ) {
//            iGpGoodOrderService.updateBatchById(orderListNew);
//        }
//        if ( itemsNew.size() > 0) {
//            iGpGoodOrderItemService.updateBatchById(itemsNew);
//        }
//        log.info("------订单自动计算金额任务结束-------");
//    }
//}
